package 剑指Offer;

import 树.BST.ListNode;

import java.util.Stack;

public class Offer06_从尾到头打印链表 {

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public int[] reversePrint(ListNode head) {
        Stack<Integer> stack = new Stack<>();

        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        //这里很重要
        int size = stack.size();
        int[] res = new int[size];

        //这里size一定要用之前保存的，不然你pop一下 stack的size在动态的变化
        for (int i = 0; i < size; i++) {
            res[i] = stack.pop();
        }
        return res;
    }
}
